﻿/****************************** Module Header ******************************\
 * Module Name:  FormDebugHelper.cs
 * Module Version:  1.0.0 (2012-8-24)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the FormDebugHelper.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.Common;
using System.Reflection;

namespace UCF.Core.Forms
{
    /// <summary>
    /// Form调试助手
    /// </summary>
    public class FormDebugHelper : IDebugHelper
    {
        /// <summary>
        /// 打印字符串
        /// </summary>
        /// <param name="s"></param>
        public void Show(string s, params object[] args)
        {
            this.ShowTextArea(args.Length == 0 ? s : s.Fmt(args));
        }


        public void Show(bool isBreakLine, bool isEnd, params object[] args)
        {
            StringBuilder sb = new StringBuilder();
            foreach (object a in args)
            {
                sb.AppendLine("{0}{1}".Fmt(a.ToString(), isBreakLine ? Environment.NewLine : string.Empty));
            }
            this.ShowTextArea(sb.ToString());
            if (isEnd) Application.ExitThread();
        }

        /// <summary>
        /// 打印实体类数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="e"></param>
        public void ShowType<T>(T e)
        {
            DataTable dt = new DataTable();
            dt.SetColumnsName("字段", "值", "类型");
            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                if (pi.CanRead) dt.Rows.Add(pi.Name, pi.GetValue(e, null), pi.PropertyType.ToString());
            }
            this.ShowTableForm(dt);
        }
        /// <summary>
        /// 弹出文本框船体
        /// </summary>
        /// <param name="s"></param>
        private void ShowTextArea(string s)
        {
            Form frm = new Form() { Text = "调试" };
            TextBox tb = new TextBox() { Multiline = true, Dock = DockStyle.Fill, Text = s };
            frm.Controls.Add(tb);
            frm.ShowDialog();
        }
        /// <summary>
        /// 弹出表格窗体
        /// </summary>
        /// <param name="dt"></param>
        private void ShowTableForm(DataTable dt)
        {
            Form frm = new Form() { Text = "调试" };
            DataGridView dgv = new DataGridView() { Dock = DockStyle.Fill, DataSource = dt };
            frm.Controls.Add(dgv);
            frm.ShowDialog();
        }

        /// <summary>
        /// 打印数据指令语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        public void ShowSql(string sql, params DbParameter[] para)
        {
            StringBuilder sb = new StringBuilder();
            Regex re;
            foreach (DbParameter p in para)
            {
                //re = new Regex("(?:(?<Test>{0})\\W|(?<Test>{0}\\Z))".Fmt(p.ParameterName));
                re = new Regex(p.ParameterName);
                if (p.DbType == DbType.Guid || p.DbType == DbType.String || p.DbType == DbType.DateTime)
                {
                    sql = re.Replace(sql, "'{0}'".Fmt(p.Value));
                }
                else
                {
                    sql = re.Replace(sql, p.Value.ToString());
                }
            }
            this.ShowTextArea(sql);
        }

        /// <summary>
        /// 打印数据参数列表
        /// </summary>
        /// <param name="para"></param>
        /// <param name="splitor"></param>
        public void ShowDbParameters(DbParameter[] para, string splitor = "<br />")
        {
            StringBuilder sb = new StringBuilder();
            foreach (DbParameter p in para)
            {
                if (p.DbType == DbType.Guid || p.DbType == DbType.String || p.DbType == DbType.DateTime)
                {
                    sb.Append("{0}='{1}'{2}".Fmt(p.ParameterName, p.Value, splitor));
                }
                else
                {
                    sb.Append("{0}={1}{2}".Fmt(p.ParameterName, p.Value, splitor));
                }
            }
            this.ShowTextArea(sb.ToString());
        }

        /// <summary>
        /// 打印数据表
        /// </summary>
        /// <param name="dt"></param>
        public void ShowTable(DataTable dt)
        {
            if (dt == null || dt.Columns.Count == 0) return;
            this.ShowTableForm(dt);
        }

        /// <summary>
        /// 打印键值集合
        /// </summary>
        /// <typeparam name="K"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="dict"></param>
        public void ShowSortedDictionary<K, V>(ref SortedDictionary<K, V> dict)
        {
            StringBuilder sb = new StringBuilder();
            foreach (K k in dict.Keys)
            {
                sb.Append("<div>{0}={1}</div>".Fmt(k, dict[k]));
            }
            this.ShowTextArea(sb.ToString());
        }

        /// <summary>
        /// 保存到日志
        /// </summary>
        /// <param name="logfolder"></param>
        /// <param name="msg"></param>
        public void Log(string logfolder, string msg)
        {
            FileHelper.AppendToFile(string.Format("{1}DateTime:{0}{1}{2}{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Environment.NewLine, msg), string.Format("{0}\\{1}.txt", logfolder, DateTime.Today.ToString("yyyyMMdd")));
        }

        /// <summary>
        /// 打印控件
        /// </summary>
        /// <param name="controlCollection"></param>
        public void ShowControls(IList<System.Web.UI.Control> controlCollection)
        {
            DataTable dt = new DataTable();
            dt.SetColumnsName("ID", "类型名称", "类型");
            foreach (System.Web.UI.Control c in controlCollection)
            {
                dt.Rows.Add(c.ID, c.GetType().Name, c.GetType().ToString());
            }
            this.ShowTableForm(dt);
        }

        public void ShowDictionary<K, V>(Dictionary<K, V> dict)
        {
            DataTable dt = new DataTable();
            dt.SetColumnsName("键", "值");
            foreach (K k in dict.Keys)
            {
                dt.Rows.Add(k, dict[k]);
            }
            this.ShowTableForm(dt);
        }

        public void ShowTypeSql<T>(T e, string primaryKey = "ID", string providerName = "System.Data.SqlClient")
        {
            this.ShowTextArea(UCF.Core.Data.ORM.EntityHelper.GetCreateInsertTextByType<T>(e, primaryKey, providerName));
        }
    }
}
